package com.gome.bg.entityDao.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.gome.bg.RowMapper.UserRowMapper;
import com.gome.bg.controller.RbBgController;
import com.gome.bg.entity.User;
import com.gome.bg.entityDao.EntityDao;
import com.gome.util.TraceInfo;
import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;

public class EntityDaoImpl implements EntityDao {
	
	 private static final String NAMESPACE="ns";  

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private MongoTemplate mongoTemplate;


	public List<Object> createQuery(String queryString) {

		return null;
	}
	@TriggersRemove(cacheName="baseCache",removeAll=true) 
	public Object save(Object model) {
		User user = (User)model;
		mongoTemplate.insert(user);   
		TraceInfo.traceInfo(EntityDaoImpl.class,"save");
		
		return null;
	}
	@TriggersRemove(cacheName="baseCache",removeAll=true)  
	public void update(Object model) {
		// TODO Auto-generated method stub

	}
	@TriggersRemove(cacheName="baseCache",removeAll=true) 
	public void delete(Object model) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Cacheable(cacheName="baseCache")
	public Object pswAuthentification( Object obj1, Object obj2) {
		
		
		
//		 User person = (User) jdbcTemplate.queryForObject(
//		 "select * from rbbguser where username=?", new Object[] { obj1 },
//		 new int[] { java.sql.Types.INTEGER }, new UserRowMapper());
	//	 return person;
		TraceInfo.traceInfo(RbBgController.class, obj2);
		User person = (User) mongoTemplate.find(new Query(), User.class);
		TraceInfo.traceInfo(EntityDaoImpl.class, person);
		if (person == null) {
			person = (User) jdbcTemplate.queryForObject("select * from rbbguser where username=?", new Object[] { obj1 }, new int[] { java.sql.Types.INTEGER },
					new UserRowMapper());
		}
		TraceInfo.traceInfo(EntityDaoImpl.class, person);
		return person;
		
		
		
		
	}
	@Cacheable(cacheName="baseCache")
	public Object getById( String obj1) {
		 User person = (User) jdbcTemplate.queryForObject(
		 "select * from rbbguser where username=?", new Object[] { obj1 },
		 new int[] { java.sql.Types.INTEGER }, new UserRowMapper());
		 return person;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}
	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}
	
	

}
